package com.ruoyi.controller;

import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.AdvBroadcast;
import com.ruoyi.service.IAdvBroadcastService;
import com.ruoyi.utils.SimpleResult;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Controller
 *
 * @author ruoyi
 * @date 2024-05-13
 */
@RestController
@RequestMapping("/system/broadcast")
public class AdvBroadcastController extends BaseController
{
    @Autowired
    private IAdvBroadcastService advBroadcastService;

    /**
     * 查询列表
     */
    @PreAuthorize("@ss.hasPermi('system:broadcast:list')")
    @GetMapping("/list")
    public TableDataInfo list(AdvBroadcast advBroadcast)
    {
        startPage();
        List<AdvBroadcast> list = advBroadcastService.selectAdvBroadcastList(advBroadcast);
        return getDataTable(list);
    }

    /**
     * 查询列表 小程序使用
     */

    @PreAuthorize("@ss.hasPermi('system:broadcast:list')")
    @GetMapping("/list/app")
    public TableDataInfo listApp(AdvBroadcast advBroadcast)
    {
        startPage();
        List<AdvBroadcast> list = advBroadcastService.selectAdvBroadcastListApp(advBroadcast);
        return getDataTable(list);
    }

    /**
     * 导出列表
     */

    @PreAuthorize("@ss.hasPermi('system:broadcast:export')")
    @Log(title = "", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AdvBroadcast advBroadcast)
    {
        List<AdvBroadcast> list = advBroadcastService.selectAdvBroadcastList(advBroadcast);
        ExcelUtil<AdvBroadcast> util = new ExcelUtil<AdvBroadcast>(AdvBroadcast.class);
        util.exportExcel(response, list, "数据");
    }

    /**
     * 获取详细信息
     */

    @PreAuthorize("@ss.hasPermi('system:broadcast:query')")
    @GetMapping(value = "/{advBroadcastId}")
    public AjaxResult getInfo(@PathVariable("advBroadcastId") Long advBroadcastId)
    {
        return success(advBroadcastService.selectAdvBroadcastByAdvBroadcastId(advBroadcastId));
    }

    /**
     * 新增
     */
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('system:broadcast:add')")
    @Log(title = "新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AdvBroadcast advBroadcast)
    {
        return toAjax(advBroadcastService.insertAdvBroadcast(advBroadcast));
    }

    /**
     * 修改
     */

    @PreAuthorize("@ss.hasPermi('system:broadcast:edit')")
    @Log(title = "", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AdvBroadcast advBroadcast)
    {
        return toAjax(advBroadcastService.updateAdvBroadcast(advBroadcast));
    }

    /**
     * 删除
     */

    @PreAuthorize("@ss.hasPermi('system:broadcast:remove')")
    @Log(title = "", businessType = BusinessType.DELETE)
	@DeleteMapping("/{advBroadcastIds}")
    public AjaxResult remove(@PathVariable Long[] advBroadcastIds)
    {
        return toAjax(advBroadcastService.deleteAdvBroadcastByAdvBroadcastIds(advBroadcastIds));
    }

    /**
     * 开播
     */

    @PostMapping("/start/{advMembersId}")
    public SimpleResult startLive(@PathVariable("advMembersId") Long advMembersId) {
        boolean b = advBroadcastService.startLive(advMembersId);
        return SimpleResult.success(b, b?"修改直播状态成功":"修改直播状态失败");
    }

    /**
     * 下播
     */

    @PostMapping("/stop/{advMembersId}")
    public SimpleResult stopLive(@PathVariable("advMembersId") Long advMembersId) {
        boolean b = advBroadcastService.stopLive(advMembersId);
        return SimpleResult.success(b, b?"修改直播状态成功":"修改直播状态失败");
    }

    /**
     * 获取设备所对应的预约直播信息
     */

    @GetMapping("/broadcast/{devicesno}/{advMembersId}")
    public SimpleResult broadcast(@PathVariable("devicesno") String devicesno,
                                  @PathVariable("advMembersId") String advMembersId) {
//        startPage();
        List<AdvBroadcast> broadcasts =
                advBroadcastService.getBroadByDevicesno(devicesno, advMembersId);
        return SimpleResult.success(broadcasts, "获取成功");
    }

    @GetMapping("/status/{advMembersId}")
    public SimpleResult status(@PathVariable("advMembersId") String advMembersId) {
        AdvBroadcast statusCode = advBroadcastService.status(advMembersId);
        return SimpleResult.success(statusCode, "获取状态成功");
    }
}
